<? 
		if(isset($_POST['btnsubmit']))
		{
			require("required/dbconnect.php"); 
			
			$description=$_POST['description'];
			$per_kg_rate=$_POST['per_kg_rate'];
			$minimum_rate=$_POST['minimum_rate'];
			$type_id=$_POST['type_id'];	
			$country=$_POST['country'];
			$all_same_rate=$_POST['all_same_rate'];					
			$insert=mysql_query("insert into os_shipping_rate(type_id,description,per_kg_rate,minimum_rate,country_id,all_same_rate)
								value('$type_id','$description','$per_kg_rate','$minimum_rate','$country','$all_same_rate')")or die(mysql_error());
			
			$id=mysql_insert_id();
			
			
			if($all_same_rate==0)
			{
				if(!empty($_POST['state'])){
					$state=$_POST['state'];
					foreach($state as $state_id)
					{
						
						mysql_query("insert into os_shipping_state(shipping_rate_id,state_id)values('$id','$state_id')")or die(mysql_error());
					}
				}
			}
			
			header("Location:shipping_view_full.php?action=4&id=$type_id");		
		}
		else
		{
				require("required/dbconnect.php"); 
				require("required/function.php"); 
				require("required/header.php"); 
				
								
				checkAccess("allow_shipping_management",1);
				if(empty($_GET["id"]))
				{
					errRedirect("2","Invalid Access",$_SERVER['PHP_SELF'],"9");
				}
				
				$id = $_GET["id"];
				
				$get_data=mysql_query("select * from os_shipping_type where id=$id");
				
				if(mysql_num_rows($get_data)==0){
					errRedirect("2","Record not found",$_SERVER['PHP_SELF'],"");		
				}
				
				
				$a=mysql_fetch_array($get_data,MYSQL_ASSOC);
?>
			<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
				
                <script language="javascript">
					function changeState(countryId) 
					{
						
						if(document.frm1.all_same_rate.value==1)
						{
							var dontg=1;
						}else{
							changeContent('../required/js/ajax_place.php?country_id='+countryId+'&tag=state[]&multiple=1','divState');
							
						}
					}
					
					function changeVisibility(type)
					{
						
						if(type==1)
						{
							document.getElementById('divAllArea').style.visibility="visible";
							document.getElementById('divState').style.visibility="hidden";
						}
						else
						{
							var countryId=document.getElementById("country").value;
							
							document.getElementById('divAllArea').style.visibility="hidden";
							document.getElementById('divState').style.visibility="visible";
							changeContent('../required/js/ajax_place.php?country_id='+countryId+'&tag=state[]&multiple=1','divState');
						}
					
					}
					
					function validateFrm(frm1){
						if(document.frm1.description.value=="")
						{
							alert("Please enter description");
							document.frm1.description.focus();
							return false;
						}
						if(document.frm1.per_kg_rate.value=="")
						{
							alert("Please enter rate per kg");
							document.frm1.per_kg_rate.focus();
							return false;
						}
						
						if(document.frm1.minimum_rate.value=="")
						{
							alert("Please enter minimum rate");
							document.frm1.minimum_rate.focus();
							return false;
						}
						
						return true;
							
					}
				</script>
				<table width="100%"  cellpadding="0" cellspacing="5" height="300">
					
					<td width="84%" align="left" valign="top">
					
						<form action="<?= $_SERVER['PHP_SELF'] ?>" name="frm1" method="post" onSubmit="return validateFrm(this)">
						<table width="100%">
						
						<td class="title" colspan="3">
						<strong>Add Shipping Rate For <?= $a['type_name'] ?></strong>
						</td>
						<tr>
							<td width="150">Description</td><td>:</td><td><input class="text" type="text" name="description" size="30" value="" maxlength="200"></td>
						</tr>
						
						<tr>
							<td>Rate Per Kg</td><td>:</td><td><?= $module_currency_code ?> <input class="text" type="text" name="per_kg_rate" size="5" value="" maxlength="5"></td>
						</tr>
                        
						<tr>
							<td>Minimum Charge</td><td>:</td><td><?= $module_currency_code ?> <input class="text" type="text" name="minimum_rate" size="5" value="" maxlength="5"></td>
						</tr>
						
						<tr valign="top">
								<td>Country</td><td>:</td>
								<td width="1507">
								
								<?
									$get_place=mysql_query("select * from os_country where status=1 order by main_country desc");
								?>
								<select name="country" id="country" class="text" onChange="changeState(this.value)">
									
								<? 
									while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
									{
										if($s['id']==$country)
										{
											$desslt="selected";
										}
										else
										{
											$desslt="";
										}
										
								?>
									<option value="<?= $s['id'] ?>" <?= $desslt ?>><?= $s['engname'] ?></option>
								<? 
										
									}
								?>
								</select>
								</td>
							  </tr>
                              	<tr>
                              		<Td>Same rate for all area</Td><Td>:</Td>
                                    <td>
                                    	<select name="all_same_rate" onchange="changeVisibility(this.value)">
                                         	<option value="0">No</option>
                                    		<option value="1">Yes</option>
                                           
                                        </select>
                                    </td>
                              	</tr>
								<tr valign="top">
									<td>State</td><td>:</td>
								<td>
                                <div id="divAllArea" style="position: absolute; visibility: hidden; float: left">The shipping rate for all areas under the selected country will be the same </div>
								<div id="divState" style="position:inherit; float: left">
								<?
									$get_place=mysql_query("select * from os_state where country_code=1 order by order_id asc");
								?>
								<select name="state[]" class="text" multiple="multiple" size="10">
									
								<? 
									while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
									{
										if($s['id']==$state)
										{
											$sslt="selected";
										}
										else
										{
											$sslt="";
										}	
								?>
									<option <?= $sslt ?> value="<?= $s['id'] ?>"><?= $s['name_eng'] ?></option>
								<? 
										
									}
								?>
								</select>
								</div>
								</td>
								</tr>
                                <tr>
                                    <td colspan="3">
                                    <input type="hidden" value="<?= $id ?>" name="type_id" />
                                    <input type="submit" value="Submit" name="btnsubmit">
                                    <input type="button" value="Cancel" onclick="window.open('shipping_view_full.php?id=<?= $id ?>','_self')">
                                    </td>
                                </tr>
						</table>
						</form>
					</td>
				 
				</table>
<? }require("required/footer.php"); ?>